<?php

App::import('Vendor', 'oauth');

class Gdata2AppModel extends AppModel {

	public $useTable = false;
/*
	public $consumerKey = 'primagain.com';
	public $consumerSecret = '7ZrM7WeS3yf3DNZx2S7Gf0P-';
*/
/*
	public $consumerKey = '241037486159-gtlgmd2bgr92a3or8r2hrdcqeiiein3v.apps.googleusercontent.com';
	public $consumerSecret = '_J9wWKXus5VZ94lbOQoV2IIw';
*/
	public $consumerKey = 'sbm-itb.ac.id';
	public $consumerSecret = 'wgh6FOKObXUx0HLkEAMwH3wP';

/*
	public $consumerKey = '864501779126-jdl3mmkr4t3vukl8pjeu1o97ii28ciod.apps.googleusercontent.com';
	public $consumerSecret = 'MASmkFxtDrzE-1PfSTfCEiDB';
*/
	
	public $consumer = null;
	public function __construct($id = false, $table = null, $ds = null) {
		$this->consumer = new OAuthConsumer($this->consumerKey, $this->consumerSecret, NULL);
	}
	
/**
 * Joins key:value pairs by inner_glue and each pair together by outer_glue
 * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE)
 * @param string $outer_glue Full URL of the resource to access
 * @param array $array Associative array of query parameters
 * @return string Urlencoded string of query parameters
 */
	function implode_assoc($inner_glue, $outer_glue, $array) {
	  $output = array();
	  
	  foreach($array as $key => $item) {
		$output[] = $key . $inner_glue . urlencode($item);
	  }
	  return implode($outer_glue, $output);
	}
	
	/**
 * Makes an HTTP request to the specified URL
 * @param string $http_method The HTTP method (GET, POST, PUT, DELETE)
 * @param string $url Full URL of the resource to access
 * @param string $auth_header (optional) Authorization header
 * @param string $postData (optional) POST/PUT request body
 * @return string Response body from the server
 */
	
	
	function request($http_method,$feed,$params=null,$postData=null,$custom_header=null,$is_upload=null,$is_resumable=null){
		$oauth = OAuthRequest::from_consumer_and_token($this->consumer, NULL, $http_method, $feed, $params);
		$oauth->sign_request(new OAuthSignatureMethod_HMAC_SHA1(), $this->consumer, NULL);
		//if(!$url){
			$url = $feed . '?' . $this->implode_assoc('=', '&', $params);
		//}
		$method = $oauth->get_normalized_http_method();
		$auth_header = $oauth->to_header();
		
		$header = array();
		if(isset($custom_header)){
			$header = array_values(array_merge(array($auth_header),$custom_header));
		}else{
			$header = array_values(array($auth_header));
		}
		//debug($header);
		if($method == 'PUT' && isset($is_upload) && $is_upload == 1 && $is_resumable == 1){
			$curl = curl_init($feed);
		}else{
			$curl = curl_init($url);
		}
		//$curl = curl_init($url);
		curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_FAILONERROR, false);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, 1); 
		curl_setopt($curl, CURLOPT_PROXY, 'proxy.sbm.itb.ac.id:8080');

		curl_setopt($curl, CURLINFO_HEADER_OUT, true);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
		//debug($url);		
		//debug($auth_header);
		
		$is_header = 0;
		switch($method) {
		case 'GET':
			curl_setopt($curl, CURLOPT_HTTPHEADER, array(
					'Content-Type: application/atom+xml', 
					$auth_header
			));
			//debug($curl);
			break;
		case 'POST':
			if(isset($is_upload) && $is_upload == 1 && !isset($is_resumable)){
				curl_setopt($curl, CURLOPT_HTTPHEADER, $header);	
			}else if(isset($is_upload) && $is_upload == 1 && $is_resumable == 1){
				curl_setopt($curl, CURLOPT_HEADER, 1);
				$is_header = 1;
				curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
			}else{
				curl_setopt($curl, CURLOPT_HTTPHEADER, array(
					'Content-Type: application/atom+xml',
					$auth_header
				));	
			}
			curl_setopt($curl, CURLOPT_POST, 1);                                       
			curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
			break;
		case 'PUT':
			if(isset($is_upload) && $is_upload == 1 && $is_resumable == 1){
				//curl_setopt($curl, CURLOPT_HEADER, 1);
				//$is_header = 1;
				curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
			}else{
				curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
			}
			curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
			break;
		case 'DELETE':
			curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header)); 
			curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method); 
			break;
		}
		$response = curl_exec($curl);
		//debug($response);
		if (!$response) {
			$response = curl_error($curl);
			
			//$url = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
			//$response = $this->request($url,'GET',$feed,$params);
		}else{
			if(!isset($is_header) || $is_header == 0){
/*
				App::import('Core', 'Xml');
				$Xml = new Xml($response);
				$response = $Xml->toArray(false);
				$Xml->__destruct();
				$Xml = null;
				unset($Xml);
*/
				
				//App::import('Core', 'Xml');
				//$Xml = Xml::build($response);
				//$response = $Xml;
				$response = Xml::toArray(Xml::build($response));
				//$Xml->__destruct();
				//$Xml = null;
				//unset($Xml);
			}
		}
		//debug(curl_getinfo($curl));
		//echo curl_getinfo($curl, CURLINFO_HTTP_CODE);
		curl_close($curl);
		//debug($response);
        return $response;
	}
}
?>
